<template>
  <div>
    <el-table :data="tableData" style="width: 100%;">
      <el-table-column label="信息ID" width="150" prop="id"></el-table-column>
      <el-table-column label="招聘部门" width="150" prop="deptType"></el-table-column>
      <el-table-column label="应聘者" width="180">
        <template slot-scope="scope">
          <el-popover placement="top" trigger="hover">
            <p>应聘人id: {{ scope.row.staff.id }}</p>
            <p>应聘人账号: {{ scope.row.staff.account }}</p>
            <p>应聘人密码: {{ scope.row.staff.passWord }}</p>
            <p>应聘人昵称: {{ scope.row.staff.nickName }}</p>
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium">{{ scope.row.staff.nickName }}</el-tag>
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="用户状态" :formatter="formatStatus" width="150" prop="userStatus"></el-table-column>
      <el-table-column label="是否入职" :formatter="formatUser" width="150" prop="staff.type"></el-table-column>
      <el-table-column label="操作" width="400">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row,0)"
                     :disabled="scope.row.userStatus == 4||scope.row.userStatus == -1">通过本轮
          </el-button>
          <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row,1)"
                     :disabled="scope.row.userStatus == 4||scope.row.userStatus == -1">流程终止
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除记录</el-button>
          <el-button size="mini" type="success" @click="handleUptate(scope.$index, scope.row)"
                     :disabled="scope.row.userStatus != 4 || scope.row.staff.type == 1">登记入职
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination :current-page="pageInfo.currentPage" :page-size="pageInfo.pageSize"
                   :page-sizes="[3, 5, 8, 20]" :total="pageInfo.total" layout="total, sizes, prev, pager, next, jumper"
                   @size-change="handleSizeChange" @current-change="handleCurrentChange">
    </el-pagination>
  </div>
</template>

<script>
export default {

  data() {
    return {
      tableData: [],
      pageInfo: {
        total: 10,
        currentPage: 4,
        pageNum: 1,
        pageSize: 8
      }
    }
  },
  mounted() {
    this.findList(1, this.pageInfo.pageSize);
  },
  methods: {
    goto() {
    },
    handleUptate(index, row) {
      alert(row.staff.nickName + "成功入职!")
      alert("入职!" + row.deptType)
      this.$http.get("/staffOnboarding?userId="+row.staff.id+"&deptName="+row.deptType).then(res=>{
        if(res!==500){
          this.$message({
            message: "修改成功!",
            type: 'success'
          });
          row.staff.type = 1;
        }
      })
    },
    handleEdit(index, row, res) {
      var num = row.userStatus
      if (res == 1) {
        num = -1;
      }
      this.$http.get("/AddRecInfo?id=" + row.id + "&res=" + num).then(res => {
        if (res.data.code !== 500) {
          if (res.data.code === 200) {
            row.userStatus = row.userStatus + 1;
            this.$message({
              message: "修改成功!",
              type: 'success'
            });
          } else {
            this.$message({
              message: '流程终止!',
              type: 'warning'
            });
            row.userStatus = -1;
          }
          console.log(res.data)
        } else {
          this.$message({
            message: '服务器故障!',
            type: 'error'
          });
          console.log(res)
        }
      })
    },
    handleDelete(index, row) {
      this.$http.get("/deletRecInfo?id=" + row.id).then(res => {
        if (res.data.code !== 500) {
          this.$message({
            message: '删除成功!',
            type: 'success'
          });
          this.tableData.splice(index, 1);
        } else {
          this.$message({
            message: '服务器故障',
            type: 'error'
          });
          console.log(res)
        }
      })
    },
    formatDate(row, column, cellValue) {
      let date = new Date(cellValue);
      return date.toLocaleString();
    },
    findList(page, limit) {
      var that = this;
      this.$http.post("/getAllRecrInfo/" + page + "/" + limit).then(function (res) {
        if (res.data.code !== 500) {
          that.tableData = res.data.data;
          that.total = res.data.total;
          that.pageInfo.total = res.data.total;
          console.log(res.data)
        } else {
          console.log(res)
        }
      })
    },
    handleSizeChange(val) {
      this.pageInfo.pageSize = val;
      this.findList(this.pageInfo.pageNum, this.pageInfo.pageSize);
    },
    handleCurrentChange(val) {
      this.pageInfo.pageNum = val;
      this.findList(this.pageInfo.pageNum, this.pageInfo.pageSize);
    },
    formatStatus(row, column, cellValue) {
      var newValue = "";
      if (cellValue == "0") {
        newValue = "安排笔试"
      } else if (cellValue == "1") {
        newValue = "技术1面"
      } else if (cellValue == "2") {
        newValue = "技术2面"
      } else if (cellValue == "3") {
        newValue = "HR面试"
      } else if (cellValue == "4") {
        newValue = "Offer!"
      } else {
        newValue = "流程终止"
      }
      return newValue;
    },
    formatUser(row, column, cellValue) {
      var newValue = "待入职";
      if (cellValue == "1") {
        newValue = "已入职"
      }
      return newValue;
    }
  }
}
</script>
